package avicit.cbb.techclass.bigsave.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

/**
 * <p>金航数码科技有限责任公司</p>
 * <p>作者：孟飞</p>
 * <p>邮箱：mengf@avicit.com</p>
 * <p>创建时间： 2014-10-11 下午4:03:13 </p>
 * <p>类说明：MongoDB服务管理类</p>
 * <p>修改记录： </p>
 */
public class MongoDBManager {
	private Properties prop = new Properties();
	
	private Map<String, MongoDBController> mongodbMap = new HashMap<String, MongoDBController>();
	
	private String defaultMongoId = "";
	
	private boolean isStart = false;
	
	public MongoDBManager(){
		ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
		try{
			InputStream in =null;
			//加载mongodb配置
			Resource res = resourceResolver.getResource("classpath:/configuration/mongodb.properties");
			in = res.getInputStream();
			if(in != null){
				prop.load(in);
				in.close();
				this.isStart = true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * MongoDB初始化，读取配置文件
	 * @author 孟飞 
	 * @throws Exception void
	 * @throws
	 */
	public void init() throws Exception{
		
		String isStartStr = this.getProperties("mongo.isstart");
		if ("false".equals(isStartStr)){
			this.isStart = false;
		} else {
			this.isStart = true;
		}
		
		if(!isStart){
			return;
		}
		
		try {
			String mongodbId = this.getProperties("mongo.id");
			this.defaultMongoId = this.getProperties("mongo.default.id");
			String[] ids = mongodbId.split(",");
			for(int i=0; i<ids.length; i++){
				String id = ids[i].trim();
				String ip = this.getProperties(id+".mongo.ip");
				int port = Integer.parseInt(this.getProperties(id+".mongo.port"));
				String dbname = this.getProperties(id+".mongo.dbname");
				String username = this.getProperties(id+".mongo.username");
				String password = this.getProperties(id+".mongo.password");
				String defaultCollection = this.getProperties(id+".mongo.default.collectionName");
				String keyfiled = this.getProperties(id+".mongo.keyfiled");
				boolean isAuth = Boolean.parseBoolean(this.getProperties(id+".mongo.isAuth"));
				
				Map<String, String> collectionMap = new HashMap<String, String>();
				String mongoModel = this.getProperties(id+".mongo.model");
				if(StringUtils.isNotBlank(mongoModel)){
					String[] models = mongoModel.split(",");
					for(int j=0; j<models.length; j++){
						String model = models[j];
						String collectionName = this.getProperties(id+".mongo.collectionName."+model);
						collectionMap.put(model, collectionName);
					}
				}
				
				MongoDBController mongoDBController = 
						new MongoDBController(ip, port, dbname, username, password, isAuth, defaultCollection, keyfiled, collectionMap);
				this.mongodbMap.put(id, mongoDBController);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 读取配置文件
	 * @author 孟飞 
	 * @param key
	 * @return String
	 * @throws
	 */
	public String getProperties(String key){
		if(prop.getProperty(key) != null){
			return prop.getProperty(key).trim();
		}
		
		return "";
	}
	
	/**
	 * 根据mongoid获取MongoDB服务
	 * @author 孟飞 
	 * @param mongoId
	 * @return MongoDBController
	 * @throws
	 */
	public MongoDBController getMongoDBController(String mongoId){
		MongoDBController mongoDbController = null;
		
		if(this.mongodbMap.get(mongoId) != null){
			mongoDbController = this.mongodbMap.get(mongoId);
		} else {
			mongoDbController = this.mongodbMap.get(this.defaultMongoId);
		}
		
		if(!mongoDbController.isConnect()){
			mongoDbController.getConnection();
		}
		
		return mongoDbController;
	}

	public boolean isStart() {
		return isStart;
	}
}
